package promis.db.connection;

import java.sql.*;
import oracle.jdbc.pool.OracleDataSource;
import org.apache.log4j.Logger;

/**
 * @author Chatree Siangklom of Administrator
 * Date =Nov 19, 2013 : 11:17:52 AM 
 * CreateBy=Chatree Siangklom
 * 
 * dbHelper
 * TODO
 */
public class dbHelper {
	private static final Logger log = Logger.getLogger(dbHelper.class);
	private Connection conn = null;	
    
    /**
     * Date =Nov 19, 2013 : 11:17:49 AM 
     * CreateBy=Chatree Siangklom
     * void
     * TODO
     * @param conn
     */
	public void setConn(Connection conn) {
		this.conn = conn;
	}

	/***
	 * Date =Nov 19, 2013 : 11:17:44 AM 
	 * CreateBy=Chatree Siangklom
	 * dbHelper
	 * TODO
	 */
	public dbHelper() {
        if(this.conn == null)
        {
        	try {
				ConnDirverOracleDataSource();
				log.info(" Continer conn is null dbHelper Connection Status :");
			} catch (SQLException e) {
				e.printStackTrace();
				log.error(e);
			}
        } else
			try {
				if(this.conn.isClosed()){
					log.info("Continer isClosed dbHelper Connection Status :");
					ConnDirverOracleDataSource();					
				}
			} catch (SQLException e) {
				e.printStackTrace();
				log.error(e);
		}
    }
    
    /*
    private synchronized void ConnDirver() throws SQLException {        
        DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
        DriverManager.setLoginTimeout(3600);
        this.conn = DriverManager.getConnection(url, user, passwd);
        
        conn.setAutoCommit(false);
    }
    */
    
	/**
	 * Date =Nov 19, 2013 : 11:17:41 AM 
	 * CreateBy=Chatree Siangklom
	 * void
	 * TODO
	 * @throws SQLException
	 */
    private synchronized void ConnDirverOracleDataSource() throws SQLException {
    	 OracleDataSource ods = new OracleDataSource();
	     ods.setURL(Userinfo.getUrl());
	     ods.setUser(Userinfo.getUser());
	     ods.setPassword(Userinfo.getPasswd());
	     ods.setConnectionCachingEnabled(true);
	     ods.setExplicitCachingEnabled(true);
	     ods.setLoginTimeout(Userinfo.getLoginTimeout());
	     conn = ods.getConnection();
	     conn.setAutoCommit(Userinfo.getAutoCommit());
    }
        
    /**
     * Date =Nov 19, 2013 : 11:17:37 AM 
     * CreateBy=Chatree Siangklom
     * Connection
     * TODO
     * @return
     */
    public synchronized Connection getConn() {
    	 if(this.conn == null)
         {
         	try {
 				ConnDirverOracleDataSource();
 				log.info(" Continer conn is null dbHelper Connection Status :");
 				System.out.println(" Continer conn is null dbHelper Connection Status :");
 			} catch (SQLException e) {
 				e.printStackTrace();
 				log.error(e.getMessage());
 			}
         } else
 			try {
 				if(this.conn.isClosed()){
 					log.info("Continer isClosed dbHelper Connection Status :");
 					System.out.println("Continer isClosed dbHelper Connection Status :");
 					ConnDirverOracleDataSource();					
 				}
 			} catch (SQLException e) {
 				e.printStackTrace();
 				log.error(e.getMessage());
 				System.out.println(e.getMessage());
 		}
        return this.conn;
    }
    
    /**
     * Date =Nov 19, 2013 : 11:18:20 AM 
     * CreateBy=Chatree Siangklom
     * void
     * TODO
     * @throws SQLException
     */
    public synchronized void CloseConn() throws SQLException {
        this.conn.close();
    }    
    
    public static void main(String [] args){
    	try{

    		dbHelper db = new dbHelper();
    		if(db.getConn().isClosed())
    			System.out.println("Connection is Close");
    		else 
    			System.out.println("Connection is Open");
    		
    	}catch(Exception e){
    		e.printStackTrace();
    	}
    }
}
